#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Data;
using Flynn.Core.Activities;
using Flynn.Core.ActivityResults;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Core.Tests.Activities
{
    [TestFixture]
    public class ExecuteSqlStatementActivityFixture
    {
        private MockRepository _mocks;

        private IExecuteSqlStatementActivity _executeSqlStatementActivity;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _executeSqlStatementActivity = new ExecuteSqlStatementActivity();
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Executing_SqlStatement_Connection_Must_Not_Be_Null()
        {
            _executeSqlStatementActivity.ExecuteSqlStatement(string.Empty, null);
        }

        [Test]
        public void Can_Execute_SqlStatement()
        {
            IDbConnection connection = _mocks.CreateMock<IDbConnection>();

            using (_mocks.Record())
            {
                IDbCommand command = _mocks.CreateMock<IDbCommand>();

                Expect.Call(connection.CreateCommand()).Return(command);

                command.CommandText = "SqlStatement";

                Expect.Call(command.ExecuteNonQuery()).Return(0);
                command.Dispose();
            }

            using (_mocks.Playback())
            {
                ExecuteSqlStatementResult result = _executeSqlStatementActivity.ExecuteSqlStatement("SqlStatement", connection);

                Assert.IsFalse(result.Failed);
            }
        }

        [Test]
        public void Executing_SqlStatement_Fails_If_Exception_Is_Thrown()
        {
            IDbConnection connection = _mocks.CreateMock<IDbConnection>();

            using (_mocks.Record())
            {
                IDbCommand command = _mocks.CreateMock<IDbCommand>();

                Expect.Call(connection.CreateCommand()).Return(command);

                command.CommandText = "SqlStatement";

                Expect.Call(command.ExecuteNonQuery()).Throw(new Exception("Failure message"));
                command.Dispose();
            }

            using (_mocks.Playback())
            {
                ExecuteSqlStatementResult result = _executeSqlStatementActivity.ExecuteSqlStatement("SqlStatement", connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }
    }
}